package mlp.game.shadownbolts;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PointF;

public class Spitfire extends Renderable {
	public float movePerKey = 20;

	public int hp = 200;
	private float targetY = 0;
	public int charge = 0;
	public boolean attacking = false;
	public boolean defence = false;
	public boolean dashing = false;
	public boolean dead = false;

	private Paint shieldFill;
	private Paint shieldStroke;
	private int attackFrame = 0;
	private float baseX = 0;
	private Shadowbolt target;
	
	private Renderable arrow;
	
	public Spitfire(Sprite sprite, float baseX, Sprite arrow){
		super(sprite);
		shieldFill = new Paint();
		shieldFill.setColor(0xFFBDFFBD);
		shieldFill.setStyle(Style.FILL);
		
		shieldStroke = new Paint();
		shieldStroke.setColor(0xFF009900);
		shieldStroke.setStyle(Style.STROKE);
		shieldStroke.setStrokeWidth(10);

		this.baseX = baseX;
		setPosition(new PointF(baseX, RenderView.visibleScreenArea.centerY()));
		
		this.arrow = new Renderable(arrow);
		updateArrowPosition();
	}
		
	@Override
	public void moveY(float y) {
		if(y < 0){
			super.moveY(0);
		}else if(y > RenderView.visibleScreenArea.height()){
			super.moveY(RenderView.visibleScreenArea.height());
		}else{
			super.moveY(y);
		}
		updateArrowPosition();
	}
	
	public void defence(){
		if(this.charge > 25 && this.attacking == false){
			this.defence = true;
		}
	}
	
	public void attack(Shadowbolt enemy){
		if(attacking == false && defence == false){
			this.target = enemy;
			this.attacking = true;
			this.attackFrame = 60;
		}
	}
	
	public void takeHit() {
		this.wasHit = true;
		this.hp -= 20;		
	}
	
	public void update(long gameTime) {
		this.moveY(getPosition().y + (targetY - getPosition().y) / 5);
		if(attacking == true){
			attackFrame--;
			if(attackFrame == 50){
				this.arrow.getSprite().play();
			}
			if(attackFrame > 40){
				this.moveX(getPosition().x + ((target.getPosition().x - arrow.getWidth() * 0.4f)- getPosition().x) / 10);
				updateArrowPosition();
			} else {
				this.moveX(getPosition().x + (baseX - getPosition().x) / 10);				
				updateArrowPosition();
			}
			if(attackFrame <= 10){
				attacking = false;	
				attackFrame = 0;
			}	
			if((super.isHit(target) || arrow.isHit(target)) && target.wasHit == false){
				target.takeHit();
				target.wasHit = true;
				target.useHitPaint = true;
			}
		}else{
			if(target != null) target.wasHit = false;
			this.moveX(getPosition().x + (baseX - getPosition().x) / 10);			
		}
		if(charge <= 0){
			defence = false;
		}
		if(charge < 100 && defence == false){
			charge++;
		}
		if(charge > 0 && defence == true){
			charge -= 2;
		}
		this.arrow.update(gameTime);
		super.update(gameTime);
	}
	
	private void updateArrowPosition(){
		this.arrow.setPosition(new PointF(getFrame().right - (getWidth() * 0.33f), getPosition().y));
	}
	
	@Override
	public void draw(Canvas canvas) {
		getSprite().debugInfo = "/HP:" + this.hp + "/DEF:" + this.defence + "/ATK:" + this.attackFrame;
		if(defence == true){
			float r = 5 + (getSprite().getWidth() / 2);
			canvas.drawCircle(getPosition().x + getSprite().getWidth() / 2, getPosition().y + getSprite().getHeight() / 2, r, shieldStroke);
			canvas.drawCircle(getPosition().x + getSprite().getWidth() / 2, getPosition().y + getSprite().getHeight() / 2, r, shieldFill);
		}
		this.arrow.draw(canvas);
		super.draw(canvas);
		if(RenderView.SHOW_DEBUG_INFO){
			canvas.drawRect(getAbsolutHitBox(), Sprite.debugPaint);
		}
	}

	public float getTargetY() {
		return targetY;
	}

	public void setTargetY(float targetY) {
		this.targetY = targetY;
	}
	
	public void moveUp(){
		this.moveUp(movePerKey);
	}
	
	public void moveDown(){
		this.moveDown(movePerKey);
	}
	
	public void moveUp(float byPx){
		this.targetY -= byPx;
		if(targetY < RenderView.visibleScreenArea.top){
			this.targetY = RenderView.visibleScreenArea.top;
		}
		if(targetY > RenderView.visibleScreenArea.bottom){
			this.targetY = RenderView.visibleScreenArea.bottom;			
		}
	}
	
	public void moveDown(float byPx){
		this.targetY += byPx;
		if(targetY < RenderView.visibleScreenArea.top){
			this.targetY = RenderView.visibleScreenArea.top;
		}
		if((targetY + getHeight()) > RenderView.visibleScreenArea.bottom){
			this.targetY = RenderView.visibleScreenArea.bottom - getHeight();			
		}
	}
		
}
